package at.fhj.swd.pse.beans;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.logging.Logger;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import at.fhj.swd.pse.domain.UserService;
import at.fhj.swd.pse.entities.User;


@Named
@Stateless
@ManagedBean
public class UserBean {

	private static final Logger logger = Logger.getLogger(UserBean.class);

	@Inject 
	private UserService userService;
	
	@Named
	private User profileData;

	@Named
	private StreamedContent profileImage;

	@Named
	private String loggedIn;
	
	@Named
	private List<User> users;

	@Inject
	private LoginBean loginBean;

	public UserBean() {

	}
	
	// Display specific user profile from get parameter
	public void displaySpecificUserProfileData() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext().getRequestParameterMap();
		String pID = params.get("userId");
		logger.info("getUserProfileData - pID: " + pID);
		if ((pID != "") && (pID != null)) {
			this.profileData = this.userService.getUserProfileByUserId(Integer.parseInt(pID));
		} else {
			this.profileData = null;
		}

	}

	@PostConstruct
	public void init() {
		this.profileData = this.loginBean.getLoggedInUser();
		if (this.loginBean.getLoggedInUser() != null && this.loginBean.getLoggedInUser().getImage() != null) {
			this.profileImage = new DefaultStreamedContent(new ByteArrayInputStream(this.loginBean.getLoggedInUser().getImage()), "image/jpeg");
		}
	}

	public User getUserProfileByUser(final User user) {
		return this.userService.getUserProfileByUserId(user.getId());
	}

	@Named
	public Boolean isLoggedIn() {
		return this.loginBean.isLoggedIn();
	}
	
	public User getActiveUser() {
		return this.loginBean.getLoggedInUser();
	}

	public void updateUser() {

		logger.info("UPDATE USER");
		logger.info("Update profile of user " + this.profileData.getUsername());

		this.userService.updateUser(this.profileData);

	}

	public void uploadUserProfileImage(final byte[] file) {
		logger.info("Upload Profile Image of user " + this.profileData.getUsername());
		this.profileData.setImage(file);
		this.userService.updateUser(this.profileData);
	}

	public User getProfileData() {
		return this.profileData;
	}

	public void setProfileData(final User profileData) {
		this.profileData = profileData;
	}

	public StreamedContent getProfileImage() {
		if (this.loginBean.getLoggedInUser() != null && this.loginBean.getLoggedInUser().getImage() != null) {
			profileImage = new DefaultStreamedContent(new ByteArrayInputStream(this.loginBean.getLoggedInUser().getImage()), "image/jpeg");
		}
		return profileImage;
	}

	public void setProfileImage(final StreamedContent profileImage) {
		this.profileImage = profileImage;
	}

	public String getLoggedIn() {
		if (this.loginBean.isLoggedIn()) {
			this.loggedIn = this.loginBean.getLoggedInUser().getUsername();
		} else {
			this.loggedIn = "Login";
		}
		return this.loggedIn;
	}

	public void setUser() {
		this.setProfileData(this.loginBean.getLoggedInUser());
		this.setProfileImage(this.getProfileImage());
	}

	@Named
	public void addFriend(User friend){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		userService.addFriend(loginBean.getLoggedInUser(), friend);		
		FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Friend successfully added!", "Friend successfully added!");
		facesContext.addMessage(null, m);
	}
	
	@Named
	public void removeFriend(User friend){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		userService.removeFriend(loginBean.getLoggedInUser(), friend);	
		FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Friend successfully removed!", "Friend successfully removed!");
		facesContext.addMessage(null, m);
	}

	public List<User> getUsers() {
		return userService.getUsers();
	}
	
	@Named
	public boolean isFriendable(final User friend) {
		if (!loginBean.getLoggedInUser().equals(friend) && (!loginBean.getLoggedInUser().getFriends().contains(friend))) {
			return true;
		}
		return false;
	}
	
	@Named
	public boolean isDeFriendable(final User friend) {
		if (!loginBean.getLoggedInUser().equals(friend) && (loginBean.getLoggedInUser().getFriends().contains(friend))) {
			return true;
		}
		return false;
	}

}
